<?php

namespace app\modules\Order\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\modules\Order\models\Order;
use app\modules\Member\models\Member;

/**
 * OrderSearch represents the model behind the search form about `app\modules\Order\models\Order`.
 */
class OrderSearch extends Order
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'source', 'user_id', 'member_id'], 'integer'],
            [['code', 'remark', 'created_at', 'status', 'updated_at', 'username', 'phone', 'company','product_id'], 'safe'],
            [['total_sale', 'sale_price', 'price'], 'number'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($product_id, $params, $show=1)
    {
        $query = Order::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            //'sort' =>  ['defaultOrder'=> ['created_at'=> SORT_DESC]]
            'sort' =>  ['defaultOrder'=> ['relation'=>SORT_DESC, 'status'=> SORT_ASC]]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

		//Yii::$app->user->can('order_all')	 //if(Yii::$app->user->identity->username != 'admin')
		
		//  订单显示范围权限验证
		if(!Yii::$app->user->can('order_all')){
			$this->member_id = Yii::$app->user->identity->id;
		}

		//$product_id = true;
		//$pnum = count($params);

		//if( $pnum == 1 && isset($params['product_id']) ){
		//		
		//	$product_id = false;
		//}elseif( $pnum > 1 ){
		//	
		//}
		//var_dump($product_id);
		//print_r($params);

		//if( ! isset($params['OrderSearch']) ){
		// if(empty($this->code) && empty($this->source) && empty($this->status) && empty($this->username) && empty($this->company)){
			
		// }
        if(empty($this->status)){
            if($show == 1){
                $query->andFilterWhere(['status'=>[1,2,3,4, 11 ,12]]);
            }
        } 
        
		//}

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'product_id' => $product_id,
            'total_sale' => $this->total_sale,
            'sale_price' => $this->sale_price,
            'price' => $this->price,
            'source' => $this->source,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'member_id' => $this->member_id,
            //'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            //'username' => $this->username,
            //'phone' => $this->phone,
        ]);
		if($this->created_at){
			$star = date("Y-m-d 00:00:00", strtotime($this->created_at));
			$end  = date("Y-m-d 23:59:59", strtotime($this->created_at));
        	$query->andFilterWhere(['between', 'created_at', $this->created_at , $end]);
		}

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }



    //订单查询 
    public function excel_search($params,$show)
    {
        $query = Order::find();

       
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            //return $dataProvider;
            return false;
        }

        //Yii::$app->user->can('order_all')  //if(Yii::$app->user->identity->username != 'admin')
        
        //  订单显示范围权限验证
        if(!Yii::$app->user->can('order_all')){
            $this->member_id = Yii::$app->user->identity->id;
        }

        //$product_id = true;
        //$pnum = count($params);

        //if( $pnum == 1 && isset($params['product_id']) ){
        //      
        //  $product_id = false;
        //}elseif( $pnum > 1 ){
        //  
        //}
        //var_dump($product_id);
        //print_r($params);

        //if( ! isset($params['OrderSearch']) ){
        // if(empty($this->code) && empty($this->source) && empty($this->status) && empty($this->username) && empty($this->company)){
            
        // }
        if(empty($this->status)){
            if($show == 1){
                $query->andFilterWhere(['status'=>[1,2,3,4, 11 ,12]]);
            }
        } 
        //}

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'product_id' => $this->product_id,
            'total_sale' => $this->total_sale,
            'sale_price' => $this->sale_price,
            'price' => $this->price,
            'source' => $this->source,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'member_id' => $this->member_id,
            //'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            //'username' => $this->username,
            //'phone' => $this->phone,
        ]);
        if($this->created_at){
            $star = date("Y-m-d 00:00:00", strtotime($this->created_at));
            $end  = date("Y-m-d 23:59:59", strtotime($this->created_at));
            $query->andFilterWhere(['between', 'created_at', $this->created_at , $end]);
        }

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $query->all();
    }

    //my index search
    public function my_search($params, $show=1)
    {
        $query = Order::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            //'sort' =>  ['defaultOrder'=> ['created_at'=> SORT_DESC]]
            'sort' =>  ['defaultOrder'=> ['relation'=>SORT_DESC, 'status'=> SORT_ASC]]
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        //Yii::$app->user->can('order_all')  //if(Yii::$app->user->identity->username != 'admin')
        
        //  订单显示范围权限验证
        if(!Yii::$app->user->can('order_all')){
            $this->member_id = Yii::$app->user->identity->id;
        }

        //$product_id = true;
        //$pnum = count($params);

        //if( $pnum == 1 && isset($params['product_id']) ){
        //      
        //  $product_id = false;
        //}elseif( $pnum > 1 ){
        //  
        //}
        //var_dump($product_id);
        //print_r($params);

        //if( ! isset($params['OrderSearch']) ){
        // if(empty($this->code) && empty($this->source) && empty($this->status) && empty($this->username) && empty($this->company)){
            
        // }

        if(empty($this->status)){
            if($show == 1){
                $query->andFilterWhere(['status'=>[1,2,3,4, 11 ,12]]);
            }
        } 
        //}

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'product_id' => $this->product_id,
            'total_sale' => $this->total_sale,
            'sale_price' => $this->sale_price,
            'price' => $this->price,
            'source' => $this->source,
            'status' => $this->status,
            'user_id' => $this->user_id,
            'member_id' => $this->member_id,
            //'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
            //'username' => $this->username,
            //'phone' => $this->phone,
        ]);
        if($this->created_at){
            $star = date("Y-m-d 00:00:00", strtotime($this->created_at));
            $end  = date("Y-m-d 23:59:59", strtotime($this->created_at));
            $query->andFilterWhere(['between', 'created_at', $this->created_at , $end]);
        }

        $query->andFilterWhere(['like', 'code', $this->code])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'company', $this->company])
            ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }
}
